<template>
  <div class="resume-edit">
    <div class="banner"></div>

    <div class="profile">
      <div class="profile-header">
        <div class="profile-header__title">
          <h1 style="color: #34373b">编辑简历</h1>
        </div>
        <div class="profile-header__back-button">
          <el-button plain round icon="el-icon-back" @click="$router.push('/resume')">返回</el-button>
        </div>
      </div>

      <div class="resumeForm" v-loading="loading">
        <el-form :model="resumeForm" ref="resumeForm" label-width="100px">
          <!-- 基本信息 -->
          <div class="section">
            <h2>基本信息</h2>
            <el-form-item label="姓名" prop="resName">
              <el-input v-model="resumeForm.resName"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="resPhone">
              <el-input v-model="resumeForm.resPhone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="resEmail">
              <el-input v-model="resumeForm.resEmail"></el-input>
            </el-form-item>
          </div>

          <!-- 期望工作 -->
          <div class="section">
            <h2>期望工作</h2>
            <el-form-item label="期望工作" prop="resExpectWork">
              <el-input v-model="resumeForm.resExpectWork"></el-input>
            </el-form-item>
          </div>

          <!-- 工作经验 -->
          <div class="section">
            <h2>工作经验</h2>
            <el-form-item label="工作经验" prop="resWorkExper">
              <el-input type="textarea" v-model="resumeForm.resWorkExper" :rows="4"></el-input>
            </el-form-item>
          </div>

          <!-- 项目经验 -->
          <div class="section">
            <h2>项目经验</h2>
            <el-form-item label="项目经验" prop="resProjectExper">
              <el-input type="textarea" v-model="resumeForm.resProjectExper" :rows="4"></el-input>
            </el-form-item>
          </div>

          <!-- 自我评价 -->
          <div class="section">
            <h2>自我评价</h2>
            <el-form-item label="自我评价" prop="resDesc">
              <el-input type="textarea" v-model="resumeForm.resDesc" :rows="4"></el-input>
            </el-form-item>
          </div>

          <el-form-item>
            <el-button type="primary" @click="submitForm">保存</el-button>
            <el-button @click="$router.push('/resume')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ResumeEdit',
  data() {
    return {
      loading: false,
      resumeForm: {
        resName: '',
        resPhone: '',
        resEmail: '',
        resExpectWork: '',
        resWorkExper: '',
        resProjectExper: '',
        resDesc: ''
      }
    }
  },
  created() {
    this.getResumeData()
  },
  methods: {
    getResumeData() {
      this.loading = true
      const userId = this.$store.state.loginInfo?.userInfo?.id
      
      if (!userId) {
        this.$message.error('请先登录')
        this.$router.push('/login')
        return
      }

      this.$axios.post("/resume/getOne", {
        jobSeekerId: userId
      }).then(res => {
        if (res.code === 200) {
          this.resumeForm = { ...this.resumeForm, ...res.data }
        }
      }).catch(err => {
        console.error('获取简历失败:', err)
        this.$message.error('获取简历信息失败')
      }).finally(() => {
        this.loading = false
      })
    },

    submitForm() {
      const userId = this.$store.state.loginInfo?.userInfo?.id
      if (!userId) {
        this.$message.error('请先登录')
        return
      }

      this.loading = true
      this.$axios.post('/resume/edit', {
        ...this.resumeForm,
        jobSeekerId: userId
      }).then(res => {
        if (res.code === 200) {
          this.$message.success('保存成功')
          this.$router.push('/resume')
        } else {
          this.$message.error(res.msg || '保存失败')
        }
      }).catch(err => {
        console.error('保存失败:', err)
        this.$message.error('保存失败')
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.banner {
  height: 300px;
  background-repeat: no-repeat;
}

.profile {
  width: 900px;
  margin: auto;
  margin-top: -200px;

  &-header {
    color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-left: 400px;
  }

  .resumeForm {
    margin-top: 30px;
    background: #fff;
    box-shadow: 0px 9px 20px 0px #c2c1c180;
    border-radius: 4px;
    padding: 30px;

    .section {
      margin-bottom: 30px;
      h2 {
        margin-bottom: 20px;
        font-size: 18px;
        color: #333;
      }
    }
  }
}
</style> 